<template>
  <div>
    <el-card style="margin: 20px 0"
      ><CategorySelect @getCategoryId="getCategoryId" :show="!scene"
    /></el-card>
    <el-card>
      <!-- 三个部分进行切换 -->

      <!-- spu列表结构 -->
      <div v-show="scene === 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!category3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" align="center" width="80">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <HintButton
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              ></HintButton>
              <HintButton
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="updateSpu(row)"
              ></HintButton>
              <HintButton
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看当前spu全部sku列表"
                @click="handler(row)"
              ></HintButton>
              <el-popconfirm title="确定要删除吗" @onConfirm="deleteSpu(row)">
                <HintButton
                  slot="reference"
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                ></HintButton>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @current-change="getSpuList"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>

      <!-- 添加SPU|修改SPU -->
      <SpuForm v-show="scene === 1" @changeScene="changeScene" ref="spu">
      </SpuForm>

      <!-- 添加SKU -->
      <SkuForm v-show="scene === 2" @changeScene="changeScene" ref="sku">
      </SkuForm>

      <!-- 展示sku列表的对话框 -->
      <el-dialog
        :title="spu.spuName + '的sku列表'"
        :visible.sync="dialogTableVisible"
        :before-close="close"
      >
        <!-- tabel展示skuList数据 -->
        <el-table
          :data="skuList"
          style="width: 100%"
          border
          v-loading="loading"
        >
          <el-table-column prop="skuName" label="名称" width="width">
          </el-table-column>
          <el-table-column prop="price" label="价格" width="width">
          </el-table-column>
          <el-table-column prop="weight" label="重量" width="width">
          </el-table-column>
          <el-table-column label="默认图片" width="width">
            <template slot-scope="{ row, $index }">
              <img
                :src="row.skuDefaultImg"
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
export default {
  name: "Spu",
  components: { SpuForm, SkuForm },
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      page: 1, // 当前页
      limit: 3, //
      total: 0, //数据总条数
      records: [], // spu列表数据
      scene: 0, // 0 展示SPU列表数据，1 添加SPU|修改SPU，2 添加SKU
      dialogTableVisible: false, // 控制sku列表对话框显示与隐藏
      spu: {}, // 存储spu数据
      skuList: [], // 存储sku列表数据
      loading: true, // loading效果
    };
  },
  methods: {
    // 自定义事件的回调
    getCategoryId(obj) {
      Object.assign(this, obj);
      if (!this.category3Id) return (this.records = []);
      this.getSpuList();
    },
    // 获取spu列表数据
    async getSpuList(page = 1) {
      this.page = page;
      let result = await this.$API.spu.reqSpuList(
        this.page,
        this.limit,
        this.category3Id
      );
      if (result.code === 200) {
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    // 每页展示的数据条数发生变化
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    // 添加Spu
    addSpu() {
      this.scene = 1;
      this.$refs.spu.addSpuData(this.category3Id);
    },
    // 修改Spu
    updateSpu(row) {
      this.scene = 1;
      this.$refs.spu.initSpuData(row);
    },
    // 子组件中点击保存或取消
    changeScene({ scene, flag, isAddSku }) {
      this.scene = scene;
      if (isAddSku) return;
      // flag区分是添加还是修改操作
      if (flag) this.getSpuList(this.page);
      else this.getSpuList(1);
    },
    // 删除Spu
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      if (result.code === 200) {
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
        this.$message.success("删除成功");
      }
    },
    // 添加sku
    addSku(row) {
      this.scene = 2;
      this.$refs.sku.getData(this.category1Id, this.category2Id, row);
    },
    // 点击查看sku列表，展示对话框
    async handler(spu) {
      this.dialogTableVisible = true;
      this.spu = spu;
      // 获取sku列表数据
      let result = await this.$API.spu.reqSkuList(spu.id);
      if (result.code === 200) {
        this.skuList = result.data;
        this.loading = false;
      }
    },
    // 关闭对话框的回调
    close(done) {
      this.loading = true;
      this.skuList = [];
      done();
    },
  },
};
</script>

<style></style>
